<?php

/**
 * @Desc:账户减值（分账减值）
 * @author dasheng@baofu.com(大圣)
 * @datetime 2025/4/6 22:11
 */
require_once "../../Init/init.php";

use App\baocaitong3\Entity\ContentBodyEntity as ContentBody;
use App\baocaitong3\Entity\HeaderEntity as Header;
use App\baocaitong3\Entity\PostParmEntity as PostParm;
use App\baocaitong3\Entity\ValuesAcctInfoEntity as ValuesAcctInfo;
use App\baocaitong3\Entity\ValuesActionEntity as ValuesAction;
use App\baocaitong3\SecurityUtil\NewRsaUtil;
use App\baocaitong3\Util\HttpClient;
use App\baocaitong3\Util\Log;
use App\baocaitong3\Util\Tools;

$ServiceTp = "BCT3-5002-001-01";
$transSerialNo = Tools::getTsnOderid("TSN");
Log::EchoFormat("商户流水号：".$transSerialNo);

$reqTime = Tools::getTime("YmdHis");
$transOrderNo = Tools::getTsnOderid("TON");
$orderNo = Tools::getTsnOderid("ORG");//商户原支付交易订单的订单号,（目前不与原单校验）
$accountType = "BALANCE";

Log::EchoFormat("减值订单号：".$transOrderNo);

$headerinfo = new Header();
$headerinfo->terminalId = $GLOBALS["TerminalId"];
$headerinfo->memberId = $GLOBALS["MemberId"];
$headerinfo->serviceTp = $ServiceTp;
$headerinfo->verifyType = "1";

$listvaie = [];
$vaie1 = new ValuesAcctInfo();
$vaie1->contractNo = "CM610000000000174898";
$vaie1->transAmount = 10.36;

$vaie2 = new ValuesAcctInfo();
$vaie2->contractNo = "CM610000000000174078";
$vaie2->transAmount = 26.35;

$listvaie[] = $vaie1;
$listvaie[] = $vaie2;

$vsae = new ValuesAction();
$vsae->version = "1.0.0";
$vsae->transSerialNo = $transOrderNo;
$vsae->reqTime = $reqTime;
$vsae->platformNo = $GLOBALS["MemberId"];
$vsae->accountType = $accountType;
$vsae->transOrderNo = $transOrderNo;
$vsae->orderNo = $orderNo;
$vsae->acctInfo = $listvaie;

$vsae->dealAmountAct();//统计总金额。

$bodycontent = new ContentBody();
$bodycontent->header = $headerinfo;
$bodycontent->body = $vsae;

$ContentStr = json_encode($bodycontent);
Log::EchoFormat("JSON序列号：".$ContentStr);
$enContent = NewRsaUtil::encryptByPfx($ContentStr,$GLOBALS["PFX_PATH"],$GLOBALS["KeyPwd"]);

$postParm = new PostParm();
$postParm->memberId = $GLOBALS["MemberId"];
$postParm->terminalId = $GLOBALS["TerminalId"];
$postParm->content = $enContent;

$ResultPost  = HttpClient::Post($postParm,"https://vgw.baofoo.com/union-gw/api/".$ServiceTp."/transReq.do");
Log::EchoFormat("返回参数：".$ResultPost);
if(empty($ResultPost)) throw new Exception("请求返回异常");


if(strpos($ResultPost,"header") != false)
{
    $resulterror = json_decode($ResultPost,true);
    var_dump($resulterror);
}else{
    $DePostString = NewRsaUtil::decrptByPub($ResultPost,$GLOBALS["CER_PATH"]);
    Log::EchoFormat("解密返回：".$DePostString);
    if(empty($DePostString))  throw new Exception("解密空值，请检查证书是否正常");
    if(strpos($DePostString,"header"))
    {
        $contenBe = json_decode($DePostString,true);
        if($contenBe["header"]["sysRespCode"] === "S_0000")
        {
            Log::EchoFormat("业务层结果:".json_encode($contenBe["body"]));
            if($contenBe["body"]["retCode"] === 1){
                /**
                 * 以状态state为准，订单以状态为准
                 */
                Log::EchoFormat("结果:state状态:".$contenBe["body"]["state"]);
            }else{
                Log::EchoFormat("查询失败:".json_encode($contenBe["body"],JSON_UNESCAPED_UNICODE));
            }
        }
    }
}

